package com.dubbo.server.controller;


import cn.hutool.core.convert.Convert;
import com.dubbo.server.service.UserService;
import com.dubbo.server.vo.PageVo;
import com.dubbo.server.vo.Pager;
import com.dubbo.server.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController

@RequestMapping("management/manage/users")
public class UsersController {

    @Autowired
    private UserService userService;

    /**
     * //     * 用户数据翻页    (有问题)
     * //
     */
    @GetMapping
    public Pager<PageVo> queryByPage(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                     @RequestParam(name = "pagesize", defaultValue = "10") Integer pageSize,
                                     @RequestParam(name = "id", required = false) Long id,
                                     @RequestParam(name = "nickname", required = false) String nickname,
                                     @RequestParam(name = "city", required = false) String city) {


        List<PageVo> pageVos = userService.queryByPage(page, pageSize, id, nickname, city);
        int counts = page * pageSize;
        Pager<PageVo> pageList = new Pager(counts, pageSize, pageSize, page, pageVos);
        return pageList;
    }


    /**
     * 用户基本资料
     *
     * @param userID
     * @return
     */
    @GetMapping("{userID}")
    public UserVo queryByUid(@PathVariable String userID) {

        return this.userService.queryByUid(userID);
    }


    /**
     * 用户冻结操作
     *
     * @param param
     * @return
     */
    @PostMapping("freeze")
//    冻结时间，1为冻结3天，2为冻结7天，3为永久冻结
//    冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
    public ResponseEntity<Object> userFreeze(@RequestBody Map<String, Object> param) {
        try {
            Long userId = Convert.toLong(param.get("userId"));
            Integer freezingTime = Convert.toInt(param.get("freezingTime"));
            Integer freezingRange = Convert.toInt(param.get("freezingRange"));
            String reasonsForFreezing = String.valueOf(param.get("reasonsForFreezing"));
            String frozenRemarks = String.valueOf(param.get("frozenRemarks"));

            Boolean bool = this.userService.userFreeze(userId, freezingTime, freezingRange, reasonsForFreezing, frozenRemarks);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 用户解冻操作
     *
     * @param param
     * @return
     */
    @PostMapping("unfreeze")
    public ResponseEntity<Object> userUnfreeze(@RequestBody Map<String, Object> param) {
        try {
            Long userId = Convert.toLong(param.get("userId"));
            String reasonsForThawing = String.valueOf(param.get("frozenRemarks"));
            Boolean bool = this.userService.userUnfreeze(userId, reasonsForThawing);
            if (bool) {
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }


}